package com.leo.basic.utils;

import com.leo.basic.scheduler.bean.SchedulerJob;
import com.leo.basic.scheduler.quartz.ConcurrentJob;
import com.leo.basic.scheduler.quartz.NonConcurrentJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;

/**
 * @ClassName SchedulerUtils
 * @Description scheduler操作工具类
 * @Author wangss
 * @date 2021.09.23 16:44
 * @Version 1.0
 */
@Slf4j
public class SchedulerUtils {

    /**
     * 判断任务是否存在
     *
     * @param scheduler
     * @param jobName
     * @param jobGroup
     * @return
     * @throws SchedulerException
     */
    public static boolean isJobExists(Scheduler scheduler, String jobName, String jobGroup) throws SchedulerException {
        if (scheduler == null) {
            return false;
        }

        if (StringUtils.isEmpty(jobGroup)) {
            jobGroup = jobName;
        }

        JobKey JobKey = new JobKey(jobName, jobGroup);
        return scheduler.checkExists(JobKey);
    }

    /**
     * 获取触发器key
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    public static TriggerKey getTriggerKey(String jobName, String jobGroup) {
        return TriggerKey.triggerKey(jobName, jobGroup);
    }

    /**
     * 获取表达式触发器
     *
     * @param scheduler
     * @param jobName
     * @param jobGroup
     * @return
     * @throws SchedulerException
     */
    public static CronTrigger getCronTrigger(Scheduler scheduler, String jobName, String jobGroup) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
        return (CronTrigger) scheduler.getTrigger(triggerKey);
    }

    /**
     * 创建任务
     *
     * @param scheduler
     * @param schedulerJob
     */
    public static void createSchedulerJob(Scheduler scheduler, SchedulerJob schedulerJob) {
        boolean isSync = false;
        if ("1".equals(schedulerJob.getIsConcurrent())) {
            isSync = true;
        }
        createSchedulerJob(scheduler, schedulerJob.getJobName(), schedulerJob.getJobGroup(), schedulerJob.getCronExpression(), isSync, schedulerJob);
    }

    /**
     * 创建定时任务
     *
     * @param scheduler
     * @param jobName
     * @param jobGroup
     * @param cronExpression
     * @param isSync
     * @param param
     */
    public static void createSchedulerJob(Scheduler scheduler, String jobName, String jobGroup, String cronExpression, boolean isSync, Object param) {
        // 并发或者非并发
        Class<? extends Job> jobClass = isSync ? ConcurrentJob.class : NonConcurrentJob.class;
    }

    public static void deleteSchedulerJob(Scheduler scheduler, String jobName, String jobGroup) throws SchedulerException {
        scheduler.deleteJob(getJobKey(jobName, jobGroup));
    }

    public static JobKey getJobKey(String jobName, String jobGroup) {
        return JobKey.jobKey(jobName, jobGroup);
    }

    public static void runOnce(Scheduler scheduler, String jobName, String jobGroup) throws SchedulerException {
        scheduler.triggerJob(getJobKey(jobName, jobGroup));
    }
}
